package com.chen106106.sstest.common.session;


import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.BinaryConnectionFactory;
import net.spy.memcached.MemcachedClient;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.beans.factory.InitializingBean;

public class MemcachedSessionManager implements HttpSessionManager,
		ServletContextAware, InitializingBean {

	public MemcachedSessionManager() {
		sessionKey = "sid";
		sessionTimeout = 600;
		cookieDomain = "zhenxi.com";
		cookiePath = "/";
	}

	public MemcachedHttpSession newSession(String sessionId) {
		MemcachedHttpSession session = new MemcachedHttpSession(sessionId,
				memcachedClient);
		session.setServletContext(servletContext);
		return session;
	}

	public MemcachedHttpSession getSession(String sessionId) {
		if (StringUtils.isEmpty(sessionId))
			return null;
		MemcachedClient client = memcachedClient;
		Object object = client.get(sessionId);
		if (object == null)
			return null;
		MemcachedHttpSession session = newSession(sessionId);
		if (object instanceof Map) {
			Map sessionMap = (Map) object;
			session.getValueMap().putAll(sessionMap);
		}
		return session;
	}

	public boolean exists(String sessionId) {
		if (StringUtils.isEmpty(sessionId))
			return false;
		else
			return memcachedClient.get(sessionId) != null;
	}

	public void updateSession(HttpSession session) {
		MemcachedHttpSession s = (MemcachedHttpSession) session;
		if (!s.isUpdated())
			s.update();
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public void afterPropertiesSet() throws Exception {
		BinaryConnectionFactory cf = new BinaryConnectionFactory();
		memcachedClient = new MemcachedClient(cf,
				AddrUtil.getAddresses(memcachedServers));
	}

	public void setMemcachedServers(String memcachedServers) {
		this.memcachedServers = memcachedServers;
	}

	public String getSessionKey() {
		return sessionKey;
	}

	public void setSessionKey(String sessionKey) {
		this.sessionKey = sessionKey;
	}

	public int getSessionTimeout() {
		return sessionTimeout;
	}

	public void setSessionTimeout(int sessionTimeout) {
		this.sessionTimeout = sessionTimeout;
	}

	public String getCookieDomain() {
		return cookieDomain;
	}

	public void setCookieDomain(String cookieDomain) {
		this.cookieDomain = cookieDomain;
	}

	public String getCookiePath() {
		return cookiePath;
	}

	public void setCookiePath(String cookiePath) {
		this.cookiePath = cookiePath;
	}

	private MemcachedClient memcachedClient;
	private String memcachedServers;
	private ServletContext servletContext;
	public String sessionKey;
	private int sessionTimeout;
	public String cookieDomain;
	public String cookiePath;
}
